/**
 * 
 */
package org.t2.backstage.module.gameserver;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.gaming.ruler.redis.RedisCache;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.tool.ListMapUtil;
import org.gaming.tool.SortUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.gameserver.model.Bulletin;
import org.t2.backstage.module.gameserver.model.GameChannel;
import org.t2.backstage.module.gameserver.model.GameChannelServer;
import org.t2.backstage.module.gameserver.model.GamePlatform;
import org.t2.backstage.module.gameserver.model.GameRegion;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.BulletinService;
import org.t2.backstage.module.gameserver.service.GameChannelServerService;
import org.t2.backstage.module.gameserver.service.GameChannelService;
import org.t2.backstage.module.gameserver.service.GamePlatformService;
import org.t2.backstage.module.gameserver.service.GameRegionService;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.gameserver.struct.DropDownOption;
import org.t2.backstage.module.gameserver.struct.ServerType;
import org.t2.backstage.module.gameserver.vo.DropDownOptions;
import org.t2.backstage.module.gameserver.vo.ServerGroupingOptions;
import org.t2.backstage.module.gameserver.vo.ServerGroupingOptions.ServerGroup;

/**
 * 查询控件
 */
@Controller
public class QueryComponentController {

	private static Logger logger = LoggerFactory.getLogger(QueryComponentController.class);
	
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private GamePlatformService gamePlatformService;
	@Autowired
	private GameChannelService gameChannelService;
	@Autowired
	private BulletinService bulletinService;
	@Autowired
	private GameRegionService gameRegionService;
	@Autowired
	private RedisCache redisCache;
	@Autowired
	private GameChannelServerService gameChannelServerService;
	
	
	@ApiDocument("请求公告下拉菜单数据")
	@RequestMapping(value = "/bulletin/options.do")
	@ResponseBody
	public DropDownOptions bulletinOptions() {
		List<Bulletin> list = bulletinService.getEntityList(0);
		SortUtil.sort(list, Bulletin::getId);
		
		DropDownOptions options = new DropDownOptions();
		for(Bulletin bulletin : list) {
			options.addOption(bulletin.getId(), bulletin.getTitle());
		}
		SortUtil.sort(options.getOptions(), DropDownOption::getValue);
		return options;
	}
	
	@ApiDocument("请求渠道下拉菜单数据")
	@RequestMapping(value = "/gameChannel/options.do")
	@ResponseBody
	public DropDownOptions channelOptions() {
		DropDownOptions options = new DropDownOptions();
		List<GameChannel> list = gameChannelService.getEntityList(0);
		for(GameChannel gameChannel : list) {
			options.addOption(gameChannel.getId(), gameChannel.getName());
		}
		SortUtil.sort(options.getOptions(), DropDownOption::getValue);
		return options;
	}
	
	@ApiDocument("请求平台下拉菜单数据")
	@RequestMapping(value = "/gamePlatform/options.do")
	@ResponseBody
	public DropDownOptions platformOptions() {
		DropDownOptions options = new DropDownOptions();
		for(GamePlatform gamePlatform : gamePlatformService.getEntityList(0)) {
			options.addOption(gamePlatform.getId(), gamePlatform.getName() + "[" + gamePlatform.getId() + "]");
		}
		SortUtil.sort(options.getOptions(), DropDownOption::getValue);
		return options;
	}
	
	@ApiDocument("服务器下拉菜单数据")
	@RequestMapping(value = "/gameServer/options.do")
	@ResponseBody
	public DropDownOptions options() {
		List<GameServer> list = gameServerService.getEntityList(0);
		
		DropDownOptions options = new DropDownOptions();
		for(GameServer gameServer : list) {
			options.addOption(gameServer.getId(),
					gameServer.getName() + "-" + gameServer.getServerId() + "(" + gameServer.getId() + ")");
		}
		return options;
	}
	
	@ApiDocument("根据渠道ID获取与之关联的服务器下拉菜单数据")
	@RequestMapping(value = "/gameServer/optionsByChannel.do")
	@ResponseBody
	public DropDownOptions optionsByChannel(@RequestParam("channelId") long channelId) {
		List<GameChannelServer> list = gameChannelServerService.getEntityList(channelId);
		DropDownOptions options = new DropDownOptions();
		for(GameChannelServer relation : list) {
			GameServer gameServer = gameServerService.getEntity(0, relation.getServerUid());
			if(gameServer == null) {
				continue;
			}
			options.addOption(gameServer.getId(),
					gameServer.getName() + "-" + gameServer.getServerId() + "(" + gameServer.getId() + ")");
		}
		return options;
	}
	
	
	@ApiDocument("将服务器根据平台分组的查询选择数据")
	@RequestMapping(value = "/gameServer/optionsByPlatform.do")
	@ResponseBody
	public ServerGroupingOptions optionsByPlatform() {
		List<GameServer> list = gameServerService.getEntityList(0);
		Map<Long, List<GameServer>> platformServers = ListMapUtil.fillListMap(list, GameServer::getPlatformId);
		
		ServerGroupingOptions options = new ServerGroupingOptions();
		
		List<Long> platformIds = new ArrayList<>(platformServers.keySet());
		for(long platformId : platformIds) {
			GamePlatform platform = gamePlatformService.getEntity(0, platformId);
			
			ServerGroup serverGroup = new ServerGroup();
			serverGroup.setLabel(platform.getName() + "[" + platform.getId() + "]");
			
			List<GameServer> servers = platformServers.get(platformId);
			SortUtil.sortInt(servers, GameServer::getServerId);
			
			for(GameServer server : servers) {
				DropDownOption option = new DropDownOption();
				option.setValue(server.getId());
				option.setLabel(server.getName() + "[" + server.getServerId() + "](" + server.getId() + ")");
				serverGroup.getChildren().add(option);
			}
			options.getGroups().add(serverGroup);
		}
		return options;
	}
	
	@ApiDocument("渠道大区下拉菜单数据")
	@RequestMapping(value = "/gameRegion/options.do")
	@ResponseBody
	public DropDownOptions regionOptions(@RequestParam("channelId") long channelId) {
		List<GameRegion> list = gameRegionService.getEntityList(channelId);
		SortUtil.sortInt(list, GameRegion::getRegionId);
		
		DropDownOptions options = new DropDownOptions();
		for(GameRegion gameRegion : list) {
			String serverDesc = ServerType.TEST.getDesc();
			if(gameRegion.getServerType() == ServerType.REVIEW.getValue()) {
				serverDesc = ServerType.REVIEW.getDesc();
			} else if(gameRegion.getServerType() == ServerType.NORMAL.getValue()) {
				serverDesc = ServerType.NORMAL.getDesc();
			}
			options.addOption(gameRegion.getRegionId(), gameRegion.getName() + "（"+ serverDesc +"）");
		}
		return options;
	}
	
	@ApiDocument("清理缓存")
	@RequestMapping(value = "/queryComponent/cleanCache.do")
	@ResponseBody
	public void cleanCache() {
		redisCache.flushAll();
		
		logger.info("清理完成");
	}
}
